package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.MainPageAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.chat.ChatActivity;
import com.kj.xuyuan.home.DynamicFragment;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.UserInfoBean;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.model.Response;
import com.stx.xhb.xbanner.XBanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.rong.imkit.utils.RouteUtils;
import io.rong.imlib.model.Conversation;

/**
 * 个人主页
 */
public class HomePageActivity extends BaseActivity {

    @BindView(R.id.banner)
    XBanner banner;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_more)
    ImageView rvMore;
    @BindView(R.id.tv_page)
    TextView tvPage;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tags)
    TextView tvTags;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_vip)
    TextView tvVip;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.tv_copy)
    TextView tvCopy;
    @BindView(R.id.tv_user_info)
    TextView tvUserInfo;
    @BindView(R.id.tv_user_info_line)
    TextView tvUserInfoLine;
    @BindView(R.id.tv_user_dynamic)
    TextView tvUserDynamic;
    @BindView(R.id.tv_user_dynamic_line)
    TextView tvUserDynamicLine;
    @BindView(R.id.cl_info)
    ConstraintLayout clInfo;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_like)
    TextView tvLike;

    private List<Fragment> fragments;
    private MainPageAdapter pageAdapter;
    private int currentPage;
    private int userId;
    private boolean isSelf;
    private UserInfoBean data;

    public static void actionStar(Context context,int userId){
        Intent intent = new Intent(context,HomePageActivity.class);
        intent.putExtra("userId",userId);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_home_page;
    }

    @Override
    protected void initView() {

        userId = getIntent().getIntExtra("userId",-1);

        if(userId == -1){
            ToastUtil.showDataErrorToast(context);
            finish();
            return;
        }

        isSelf = userId == SPUtils.getIntData(context, Constants.UID,-1);

        if(isSelf){
            llBottom.setVisibility(View.GONE);
        }else{
            llBottom.setVisibility(View.VISIBLE);
        }

        fragments = new ArrayList<>();
        fragments.add(new MyDataFragment());
        fragments.add(DynamicFragment.newInstance(3,userId+""));
        pageAdapter = new MainPageAdapter(getSupportFragmentManager(),fragments);
        vpContent.setAdapter(pageAdapter);

        getUserData();
    }

    private void getUserData() {
        Map map = new HashMap();
        if(!isSelf){
            map.put("id", userId+"");
        }

        OkGoUtil.postRequest(getContext(), UrlConstants.GET_USER_INFO, map, new JsonCallback<BaseModel<UserInfoBean>>() {
            @Override
            public void onSuccess(Response<BaseModel<UserInfoBean>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        data = response.body().getData();
                        setView();
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
            }

            @Override
            public void onError(Response<BaseModel<UserInfoBean>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
            }
        });
    }

    private void setView() {
        tvName.setText(data.getNickname());
        setUserInfoText();

        Drawable drawable = null;
        if(data.getGender() == 1){//男
            drawable = getResources().getDrawable(R.mipmap.icon_boy);
            tvSex.setBackgroundResource(R.drawable.shape_blue_20);
        }else{
            tvSex.setBackgroundResource(R.drawable.shape_pink_corners_30);
            drawable = getResources().getDrawable(R.mipmap.icon_girl);
        }
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        tvSex.setCompoundDrawables(drawable,null,null,null);
        tvSex.setText(data.getYears());
        tvId.setText("絮缘号："+data.getIdno());

        if(!isSelf){
            tvLike.setText(data.getIs_like() == 0 ? "喜欢" : "发消息");
        }
    }

    private void setUserInfoText() {
        StringBuffer sb = new StringBuffer();
        if(!StringUtils.isEmpty(data.getYears())){
            sb.append(data.getYears()).append("|");
        }
        if(!StringUtils.isEmpty(data.getProvince())){
            sb.append(data.getProvince()).append("|");
        }
        if(!StringUtils.isEmpty(data.getHeight())){
            sb.append(data.getHeight()).append("cm").append("|");
        }
        if(!StringUtils.isEmpty(data.getIncome_start())){
            sb.append(data.getIncome_start()).append("-").append(data.getIncome_end()).append("|");
        }
        if(!StringUtils.isEmpty(data.getEducation())){
            sb.append(data.getEducation()).append("|");
        }
        if(sb.length() > 0){
            sb.deleteCharAt(sb.length()-1);
            tvTags.setText(sb.toString());
        }else{
            tvTags.setText("暂未填写");
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return -1;
    }

    @OnClick({R.id.iv_back, R.id.rv_more, R.id.cl_info, R.id.tv_user_dynamic_line, R.id.tv_user_dynamic, R.id.tv_user_info_line,
            R.id.tv_user_info,R.id.tv_like})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rv_more:
                break;
            case R.id.cl_info:
                break;
            case R.id.tv_user_info:
            case R.id.tv_user_info_line:
                if(currentPage != 0){
                    currentPage = 0;
                    tvUserInfo.setTextSize(21);
                    tvUserDynamic.setTextSize(18);
                    tvUserInfo.setTextColor(getResources().getColor(R.color.color_333));
                    tvUserDynamic.setTextColor(getResources().getColor(R.color.gray9));
                    tvUserInfoLine.setVisibility(View.VISIBLE);
                    tvUserDynamicLine.setVisibility(View.INVISIBLE);
                    vpContent.setCurrentItem(currentPage);
                }
                break;
            case R.id.tv_user_dynamic:
            case R.id.tv_user_dynamic_line:
                if(currentPage != 1){
                    currentPage = 1;
                    tvUserInfo.setTextSize(18);
                    tvUserDynamic.setTextSize(21);
                    tvUserInfo.setTextColor(getResources().getColor(R.color.gray9));
                    tvUserDynamic.setTextColor(getResources().getColor(R.color.color_333));
                    tvUserInfoLine.setVisibility(View.INVISIBLE);
                    tvUserDynamicLine.setVisibility(View.VISIBLE);
                    vpContent.setCurrentItem(currentPage);
                }
                break;
            case R.id.tv_like:
                if(!isSelf){
                    if(data.getIs_like() == 0){
                        like();
                    }else{
                        toChat();
                    }
                }
                break;
        }
    }

    private void toChat() {
        ChatActivity.actionStar(context, Conversation.ConversationType.PRIVATE,data.getRy_id());
    }

    private void like() {
        showDialog();
        Map map = new HashMap();
        map.put("for_user_id",userId+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.LIKE, map, new JsonCallback<BaseModel<?>>() {
            @Override
            public void onSuccess(Response<BaseModel<?>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        data.setIs_like(1);
                        tvLike.setText("发消息");
                    }
                    ToastUtil.showToast(getContext(),response.body().getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<BaseModel<?>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                dismissDialog();
            }
        });
    }
}